<?php

namespace Modules\User\Http\Controllers\Admin;


use Illuminate\Http\Request;
use Modules\User\Http\Controllers\UserBaseController;
use Modules\User\Models\AdminUser;
use Modules\User\Repositories\AdminUserRepository;
use Modules\User\Services\AuthorizationManageServer;
use Psr\Http\Message\ServerRequestInterface;

/**
 * 后台用户认证控制器
 * @author long2ge
 * @date 2023/10/24 2:08 PM
 * @class AdminUserAuthController
 * @package Modules\User\Http\Controllers\AdminUser
 * @describe
 *
 * @OA\Tag(
 *     name="后台用户授权",
 *     description="后台用户的注册，登录，退出",
 *     @OA\ExternalDocumentation(
 *         description="后台用户授权相关的路由",
 *         url="http://swagger.io"
 *     )
 * )
 *
 */
class AdminUserAuthController extends UserBaseController
{
    /**
     * @OA\Post(
     *     path="/api/admin/user/login",
     *     tags={"后台用户授权"},
     *     summary="登陆",
     *     description="后台用户登陆接口",
     *     @OA\RequestBody(required=true, @OA\JsonContent(
     *           required={"username", "password"},
     *           @OA\Property(property="username", type="string", description="账号", example="admin"),
     *           @OA\Property(property="password", type="string", description="密码", example="123456"),
     *     )),
     *     @OA\Response(
     *         response=200,
     *         description="SUCCESS/成功",
     *         @OA\MediaType(
     *             mediaType="application/json",
     *             @OA\Schema(
     *              @OA\Property(property="token_type", type="string", description="token类型"),
     *              @OA\Property(property="expires_in", type="integer", format="int32", description="31536000"),
     *              @OA\Property(property="access_token", type="string", description="访问token"),
     *              @OA\Property(property="refresh_token", type="string", description="刷新token"),
     *         ),
     *         example={"token_type": "Bearer","expires_in": 31536000,"access_token": "xxxx", "refresh_token":"xxx"}
     *        )
     *     ),
     * )
     */
    public function login(ServerRequestInterface $request)
    {
        $params = (array) $request->getParsedBody();
        $params = [
            'username' => $params['username'] ?? null,
            'password' => $params['password'] ?? null,
        ];

        $this->validateWithArray($params, [
            'username' => 'required|string',
            'password' => 'required|string'
        ]);

        $authorizationManageServer = new AuthorizationManageServer(new AdminUserRepository());

        return $authorizationManageServer->login($request, $params['username'], $params['password']);
    }


    /**
     * @OA\Post(
     *     path="/api/admin/user/register",
     *     tags={"后台用户授权"},
     *     summary="注册",
     *     description="后台用户注册接口",
     *     @OA\RequestBody(required=true, @OA\JsonContent(
     *           required={"username", "password"},
     *           @OA\Property(property="username", type="string", description="账号", example="admin"),
     *           @OA\Property(property="password", type="string", description="密码", example="123456"),
     *     )),
     *     @OA\Response(response=400, description="注册失败"),
     *     @OA\Response(
     *          response=204,
     *          description="注册成功"
     *     )
     * )
     */
    public function register(Request $request)
    {
        $params = $request->only([
            'username',
            'password',
        ]);

        $this->validateWithArray($params, [
            'username' => 'required|string',
            'password' => 'required|string'
        ]);

        AdminUser::registerByPassword($params['username'], $params['password']);

        return $this->noContent();
    }

    /**
     * @OA\Post(
     *     path="/api/admin/user/logout",
     *     tags={"后台用户授权"},
     *     summary="退出登陆",
     *     description="后台用户退出登陆接口",
     *      @OA\Parameter(
     *          name="Accept",
     *          description="application/json",
     *          example="application/json",
     *          required=true,
     *          in="header",
     *          @OA\Schema(
     *              type="string"
     *          )
     *      ),
     *      @OA\Parameter(
     *          name="Authorization",
     *          description="Bearer token",
     *          example="Bearer xxxxx",
     *          required=true,
     *          in="header",
     *          @OA\Schema(
     *              type="string"
     *          )
     *      ),
     *     @OA\Response(response=400, description="退出失败"),
     *     @OA\Response(
     *          response=204,
     *          description="退出成功"
     *     )
     * )
     */
    public function logout(Request $request)
    {
        $request->user()->logout();

        return response()->noContent();
    }


}
